package com.bydq.patrolsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bydq.patrolsystem.entity.PsLineInfo;
import com.bydq.patrolsystem.entity.PsSystemconfig;
import com.bydq.patrolsystem.service.PsLineInfoService;
import com.bydq.patrolsystem.service.PsPoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("line")
public class LineController {

    @Autowired
    private PsLineInfoService psLineInfoService;

    @Autowired
    private PsPoleInfoService psPoleInfoService;

    @RequestMapping("/lineindex")
    public String lineIndex(){
        return "line";
    }

    @ResponseBody
    @RequestMapping("/getLine")//获取线路数据列表
    public Map<String,Object> getLine(Integer page,Integer limit,String lineCode,Integer systemConfig){

        page=page==null?1:page;
        limit=limit==null?5:limit;

        IPage<PsLineInfo> p=new Page<>(page,limit);

        QueryWrapper<PsLineInfo> qw=new QueryWrapper<>();
        qw.like(lineCode!=null,"lineCode",lineCode);
        qw.eq(systemConfig!=null,"runningId",systemConfig);

        psLineInfoService.page(p,qw);

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("count",p.getTotal());
        map.put("data",p.getRecords());

        return map;
    }

    @ResponseBody
    @RequestMapping("/getSystemconfig")//获取运行状态名
    public List<PsSystemconfig> getSystemconfig(){
        return psLineInfoService.getSystemconfig();
    }

    @ResponseBody
    @RequestMapping("/upIsStart")//启动/停用
    public boolean upIsStart(int id,int isStart){

        PsLineInfo line=psLineInfoService.getById(id);
        if(isStart==1){
            line.setIsStart(0);
        }
        else{
            line.setIsStart(1);
        }
        return psLineInfoService.updateById(line);
    }

    @ResponseBody
    @RequestMapping("/del")//删除
    public boolean del(int id){

        boolean isOk=false;
        //删除指定路线的同时
        if(psLineInfoService.removeById(id)){
            //删除该路线的所有杆塔
            if(psPoleInfoService.delByLineId(id)){
                isOk=true;
            }
        }
        return isOk;
    }

    @RequestMapping("/edit")
    public String edit(Integer id,Model m){

        if(id!=null){
            m.addAttribute("date",psLineInfoService.getById(id));
        }

        return "line_Update";
    }

    @ResponseBody
    @RequestMapping("/upDate")
    public boolean upDate(Long id,String lineCode,String lineName,Double lineLength,Double backLength,
                          Date productDate,String voltageLevel,String startPoleCode,String endPoleCode,Long towerBaseNum){

        System.out.println(lineName);
        System.out.println(productDate);
        System.out.println(id);


        return false;
    }
}
